package com.leejxx.health.service.impl;

import com.leejxx.health.dao.MemberDao;
import com.leejxx.health.dao.OrderDao;
import com.leejxx.health.dao.ReportDao;
import com.leejxx.health.pojo.MemberReport;
import com.leejxx.health.pojo.Order;
import com.leejxx.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author LeeJx
 * @ClassName ReportServiceImpl
 * @Description TODO
 * @date 2021/1/10 12:43
 * @Version 1.0
 */
@DubboService
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    @Transactional
    public MemberReport getMemberReport() {
        List<Date> dates = reportDao.getDates();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        List<String> months = new ArrayList<>();
        for (Date date : dates) {
            months.add(dateFormat.format(date));
        }

        //去除重复月份
        months = months.stream().distinct().collect(Collectors.toList());

        //获得每个的会员数量
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            memberCount.add(reportDao.getMemberCount(month));
        }

        //计算每个月的总量
        for (int i = 1; i < memberCount.size(); i++) {
            memberCount.set(i, memberCount.get(i) + memberCount.get(i - 1));
        }
        List<Integer> newMemberCount = new ArrayList<>(memberCount);

        return new MemberReport(months, newMemberCount);
    }

    @Override
    public Map<String, Object> getSetMealReport() {
        List<String> setMealNames = reportDao.getSetMealName();

        List<Map<String, String>> setMealCount = new ArrayList<>();

        for (String setMealName : setMealNames) {
            Map<String, String> countMap = new HashMap<>();
            countMap.put("name", setMealName);
            countMap.put("value", String.valueOf(reportDao.getSetMealCount(setMealName)));
            setMealCount.add(countMap);
        }

        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("setmealNames", setMealNames);
        map.put("setmealCount", setMealCount);
        return map;
    }

    @Override
    @Transactional
    public Map<String, Object> getBusinessReportData() {
        Map<String, Object> resultMap = new HashMap<>();

        //日期处理
        Calendar today = Calendar.getInstance();
        //将时分秒毫秒清零
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        //设置周一为每周第一天
        today.setFirstDayOfWeek(Calendar.MONDAY);
        //本周第一天
        Calendar weekFirstDay = (Calendar) today.clone();
        weekFirstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        //本周最后一天
        Calendar weekLastDay = (Calendar) today.clone();
        weekLastDay.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        //本月第一天
        Calendar monthFirstDay = (Calendar) today.clone();
        monthFirstDay.set(Calendar.DAY_OF_MONTH, 1);
        //本月最后一天
        Calendar monthLastDay = (Calendar) today.clone();
        int maxDayOfMonth = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthLastDay.set(Calendar.DAY_OF_MONTH, maxDayOfMonth);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //会员数据
        //日期
        resultMap.put("reportDate", dateFormat.format(today.getTime()));
        //今日新增会员
        resultMap.put("todayNewMember", memberDao.countByRegTimeRange(today.getTime(), today.getTime()));
        //本周新增会员
        resultMap.put("thisWeekNewMember", memberDao.countByRegTimeRange(weekFirstDay.getTime(), weekLastDay.getTime()));
        //本月新增会员
        resultMap.put("thisMonthNewMember", memberDao.countByRegTimeRange(monthFirstDay.getTime(), monthLastDay.getTime()));
        //总会员
        resultMap.put("totalMember", memberDao.countByRegTimeBefore(dateFormat.format(today.getTime())));

        //预约数据
        //今日预约人数
        resultMap.put("todayOrderNumber", orderDao.countByOrderDateRange(today.getTime(), today.getTime(), null));
        //本周预约人数
        resultMap.put("thisWeekOrderNumber", orderDao.countByOrderDateRange(weekFirstDay.getTime(), weekLastDay.getTime(), null));
        //本月预约人数
        resultMap.put("thisMonthOrderNumber", orderDao.countByOrderDateRange(monthFirstDay.getTime(), monthLastDay.getTime(), null));
        //今日到诊人数
        resultMap.put("todayVisitsNumber", orderDao.countByOrderDateRange(today.getTime(), today.getTime(), Order.ORDERSTATUS_YES));
        //本周到诊人数
        resultMap.put("thisWeekVisitsNumber", orderDao.countByOrderDateRange(weekFirstDay.getTime(), weekLastDay.getTime(), Order.ORDERSTATUS_YES));
        //本月到诊人数
        resultMap.put("thisMonthVisitsNumber", orderDao.countByOrderDateRange(monthFirstDay.getTime(), monthLastDay.getTime(), Order.ORDERSTATUS_YES));
        //热门套餐
        //      hotSetmeals ->  [ {"proportion":0.4545,"name":"粉红珍爱(女)升级TM12项筛查体检套餐","setmeal_count":5}]	// 热门套餐
        resultMap.put("hotSetmeal", orderDao.countByHotSetMeal(4));

        return resultMap;
    }
}
